package sdk.webview.fmc.com.fmcsdk.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class DateUtil {
    /*
     * 将时间转换为时间戳
     */
    public static long dateToStamp(String time) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        try {
            Date date = simpleDateFormat.parse(time);
            long ts = date.getTime();
            return ts;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return System.currentTimeMillis();
    }

    /*
     * 将时间转换为时间戳
     */
    public static long dateToStamptime(String time) {
        String tempTime;
        SimpleDateFormat simpleDateFormat;
        if (time.contains(".")) {
            simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
            tempTime = time.substring(0, 8);
        } else {
            tempTime = time;
            simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        }
        try {
            Date date = simpleDateFormat.parse(tempTime);
            long ts = date.getTime();
            return ts;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return System.currentTimeMillis();
    }

    /**
     * 获取手机时间
     *
     * @return
     */
    public static Date getServerDate(Context context) {
        if (context == null) {
            return new Date();
        }
        Date serverDate = new Date(System.currentTimeMillis());
        return serverDate;
    }


    /***
     * 获取当前日期
     */
    public static String getCurrentDate() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");// HH:mm:ss
//获取当前时间
        Date date = new Date(System.currentTimeMillis());

        return simpleDateFormat.format(date);
    }

    public static String getCurrentDate(String format){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);// HH:mm:ss
        //获取当前时间
        Date date = new Date(System.currentTimeMillis());

        return simpleDateFormat.format(date);
    }


    public static String StringData(Context context) {
        final Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        String mYear = String.valueOf(c.get(Calendar.YEAR)); // 获取当前年份
        String mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份
        String mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));// 获取当前月份的日期号码
        String mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
        String hour = String.valueOf(c.get(Calendar.HOUR_OF_DAY));
        String minute = String.valueOf(c.get(Calendar.MINUTE));
        if (minute.length() == 1) {
            minute = "0" + minute;
        }

        SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
        if (sharedPrefs.getInt("lang", 0) == 1) {
            if ("1".equals(mWay)) {
                mWay = "Sunday";
            } else if ("2".equals(mWay)) {
                mWay = "Monday";
            } else if ("3".equals(mWay)) {
                mWay = "Tuesday";
            } else if ("4".equals(mWay)) {
                mWay = "Wednesday";
            } else if ("5".equals(mWay)) {
                mWay = "Thursday";
            } else if ("6".equals(mWay)) {
                mWay = "Friday";
            } else if ("7".equals(mWay)) {
                mWay = "Saturday";
            }


            return mYear + "-" + mMonth + "-" + mDay + "   " + hour + ":" + minute + "  " + mWay;
        } else {
            if ("1".equals(mWay)) {
                mWay = "日";
            } else if ("2".equals(mWay)) {
                mWay = "一";
            } else if ("3".equals(mWay)) {
                mWay = "二";
            } else if ("4".equals(mWay)) {
                mWay = "三";
            } else if ("5".equals(mWay)) {
                mWay = "四";
            } else if ("6".equals(mWay)) {
                mWay = "五";
            } else if ("7".equals(mWay)) {
                mWay = "六";
            }
            return mYear + "-" + mMonth + "-" + mDay + "   " + hour + ":" + minute + "  星期" + mWay;
        }


    }
}